package com.hongkzh.www.conmon;

import com.auth0.jwt.interfaces.Claim;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;

import java.util.Map;

public class AuthInterceptor implements Interceptor {
    @Override
    public void intercept(Invocation inv) {
        Controller c = inv.getController();
        try {
            c.getFiles();
        } catch (Exception e) {
            //e.printStackTrace();
        }

        String token = c.getPara("loginUid");
        if (c.isParaExists("loginUid")) {
            if (StrKit.notBlank(token)) {
                Map<String, Claim> map = JwtUtil.unsign(token);
                if (map != null) {
                    System.out.println(map.get("user_id").asString());
                    c.setAttr("loginUid", map.get("user_id").asString());
                    inv.invoke();
                }
                else{
                    ReqResponse<String> r=new ReqResponse();
                    r.setCode(1);
                    r.setMsg("用户未登录");
                    c.renderJson(r);
                }
            }
            else{
                ReqResponse<String> r=new ReqResponse();
                r.setCode(1);
                r.setMsg("用户未登录");
                  c.renderJson(r);
            }

        } else {
            inv.invoke();
        }


    }
}
